// pages/operat/fleedTools/inStorage/scanCode/scanCode.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
 data: {
 returnInfo: {},
 myDate: 0
 },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var returnInfo = {};
  if (options.code != undefined) {
   let code = [options.code];
   if (code) {
    returnInfo.Codes = code;
    returnInfo.Num = code.length;
    returnInfo.ProductID = options.ProductId
   }
  } else {
   returnInfo.Codes = null;
   returnInfo.Num = 0;
   returnInfo.ProductID = 0
  }
  this.setData({
   returnInfo: returnInfo
  })
 },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    if (this.data.myDate == 1) {
      this.setData({
        returnInfo: {}
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
 
  continueOper: function () {
   let that = this;
   wx.scanCode({
    success: (res) => {
     var codes = that.data.returnInfo.Codes;
     if (codes) {
       if (codes.indexOf(res.result) > -1) {
         wx.showToast({
           title: "该码已重复扫描",
         })
         return
       }
       }
     if (!that.data.returnInfo.ProductID) { that.data.returnInfo.ProductID=0;}
     LApp._get({
      url: 'nbapi/fleedtool/th/check',
      data: { code: res.result, userId: LApp.globalData.userInfo.UserID, proId: that.data.returnInfo.ProductID },
      success: function (r) {
       if (r) {
        if (codes == null) codes = [];
        codes.push(res.result);
        var returnInfo = {};
        returnInfo.Codes = codes;
        returnInfo.Num = returnInfo.Codes.length;
        returnInfo.ProductID = r.ProductId;
        that.setData({
         returnInfo: returnInfo
        })
       }
       else {
        wx.showToast({
         title: r,
        })
       }
      }
     })

    }
   })
  },
  returnStorage: function () {
    let _this = this;
   // _this.data.returnInfo.FormCode = 'TH' + new Date().getTime();
        //页面传值并跳转
        if (_this.data.returnInfo) {
          LApp.pages.add(_this);
          wx.navigateTo({
            url: '../returnApply/returnApply',
          })
        }  
  }
  // returnStorage: function () {
  //  let _this = this;
  //  LApp._get({
  //   url: "nbapi/fleedtool/getinfo/formcode",
  //   data: { formType: 'TH', enterpriseId: LApp.globalData.userInfo.EnterpriseId },
  //   success: function (res) {
  //    _this.data.returnInfo.FormCode = res;
  //    //页面传值并跳转
  //    if (_this.data.returnInfo) {
  //     LApp.pages.add(_this);
  //     wx.navigateTo({
  //      url: '../returnApply/returnApply',
  //     })
  //    }
  //   }
  //  })

  // }
})